////
////  proCate.swift
////  WxToutiao
////
////  Created by YQ05 on 2018/4/9.
////  Copyright © 2018年 yons. All rights reserved.
////
//
//
//import Foundation
//import ObjectMapper
//import Moya
//
//class productCat: Mappable {
//    var name: String?
//    //字符串写成可选的
//    var ID: Int = 0
//    //基本数据类型要赋个初值
// var procates : [Procates]!
//
//    required init?(map: Map) {
//
//    }
//    func mapping(map: Map) {
//
//        name <- map["productCat.1.name"]
//        ID <- map["productCat.1.ID"]
//
//    }
//
//}
//
//
//
//
//struct proCateResponse: Mappable{
////        var name : String!
////        var ID : String!
//    //    var count : Int!
//    var name: String? //字符串写成可选的
//    var ID: Int = 0  //基本数据类型要赋个初值
//    var productCat : [productCat]?         // Users数组
//    //var newsCat : [newsCat]?
//
//    var procates : [productCat]!
//
//
//    init?(map: Map) {
//
//    }
//    mutating func mapping(map: Map) {
//       //newsCat <- map["newsCat"]
//       productCat <- map["productCat"]
//
//       procates <- map["productCat"]
//       name <- map["productCat.0.name"]
//       ID <- map["productCat.0.ID"]
//
//    }
//}
//
//
//
//struct Procates: Mappable{
//    var ID : Int!
//    var name : String!
//    var procates : [Procates]!
//
//    var productCat: [productCat]?
//    //var newsCat: [newsCat]?
//
//    init?(map: Map) {
//
//    }
//    mutating func mapping(map: Map) {
//        name <- map["productCat.1.name"]
//        ID <- map["productCat.1.ID"]
//        //newsCat <- map["newsCat"]
//        productCat <- map["productCat"]
//        procates <- map["productCat"]
//
//
//        //image = img![0] as! String
//    }
//}
//
//func getprocateList(){
//    let provider = MoyaProvider<NetworkService2>()
//    provider.request(.category) { (result) in
//        switch result {
//        case let .success(moyaResponse):
//            let json = try! moyaResponse.mapJSON() as! [String:Any]
//            //let json = try! moyaResponse.mapJSON() as! String
//            //if let users:[pro2] = Mapper<pro2>().mapArray(JSONString: json){
//            if let jsonResponse = proCateResponse(JSON: json){
//               print("返回的标题\(jsonResponse.ID)")
//                print(jsonResponse.name ?? 36)
//
//               // print(jsonResponse.friends?.count.description as! String)
////                let json1 = "{\"productCat\":[{\"ID\":41,\"name\":\"家居用品\"},{\"ID\":38,\"name\":\"家用電器\"},{\"ID\":34,\"name\":\"數碼配件\"},{\"ID\":40,\"name\":\"服裝飾物\"},{\"ID\":36,\"name\":\"精品\"},{\"ID\":35,\"name\":\"美容護理\"},{\"ID\":42,\"name\":\"課程\"},{\"ID\":39,\"name\":\"運動用品\"}]}"
////                let users:[pro2] = Mapper<pro2>().mapArray(JSONString: json1)!
////                print(users.count)
//            }
//
//
//        case .failure:
//            print("wangluocuowu2")
//
//        }
//    }
//}
//
//extension Procates{
//
//    //获取产品分类列表
//    static func request(completion:@escaping ([Procates]?) -> Void){
//
//        let provider = MoyaProvider<NetworkService2>()
//        provider.request(.category) { result in
//            switch result {
//            case let .success(moyaResponse):
//                let json = try! moyaResponse.mapJSON() as! [String:Any]
//
//                if let procatejsonResponse = proCateResponse(JSON: json){
//                    //print("获取到的产品分类z\(shopjsonResponse.products)")
//                    //print(procatejsonResponse.friends?.count.description as! String)
////                    print("返回的标题\(procatejsonResponse.ID)")
////                    print(procatejsonResponse.name ?? 36)
//                    let poac = procatejsonResponse.productCat?.count.description as! String
//                    //print("newsCat : \(procatejsonResponse.newsCat?.count.description as! String)")
//                    print("productCat : \(poac)")
//                    //completion(procatejsonResponse.procates)
//                    //completion(procatejsonResponse.procates)
//
//
//
//                }
//            // do something with the response data or statusCode
//            case  .failure:
//                print("网络错误")
//                completion(nil)
//                // this means there was a network failure - either the request
//                // wasn't sent (connectivity), or no response was received (server
//                // timed out).  If the server responds with a 4xx or 5xx error, that
//                // will be sent as a ".success"-ful response.
//            }
//        }
//
//    }
//}
//
//
//class User: Mappable {
//    var username: String?
//    var age: Int?
//    var weight: Double!
//
//    init(){
//    }
//
//    required init?(map: Map){
//        // 检查JSON中是否有"username"属性
//        if map.JSON["username"] == nil {
//            return nil
//        }
//    }
//
//    // Mappable
//    func mapping(map: Map) {
//        username    <- map["username"]
//        age         <- map["age"]
//        weight      <- map["weight"]
//    }
//}
//
//class pro2: Mappable {
//    var name: String?
//    var id: Int?
//
//
//    init(){
//    }
//
//    required init?(map: Map){
//    //     检查JSON中是否有"username"属性
////        if map.JSON["productCat"] == nil {
////            return nil
////        }
//    }
//
//    // Mappable
//    func mapping(map: Map) {
//        name <- map["name"]
//        id   <- map["ID"]
//
//    }
//}

//
//  Category.swift
//  WxToutiao
//
//  Created by yons on 2017/5/20.
//  Copyright © 2017年 yons. All rights reserved.
//

import Foundation
import Moya

//struct productCatIndexResponse: Codable {
//    //var id = ""
//    //var name :String
//    var status = ""
//    var count = 0
//    var productCat : [ProductCat]!
//}
//
//struct ProductCat: Codable {
//    //var id = ""
//    var name : String
//
//}



//struct productCat3IndexResponse: Codable {
//
//    var productCat : [productcat]!
//    var newsCat : [newscat]!
//}
//
//struct productcat: Codable {
//    //var id = ""
//    var name : String
//    var ID = 0
//
//}
//struct newscat: Codable {
//    //var id = ""
//    var name : String
//    var ID = 0
//
//}

struct homeCateIndexResponse: Codable {
    
    var productCat : [productcat]!
    var newsCat : [newscat]!
}

struct productcat: Codable {
    //var id = ""
    var name : String
    var ID = 0
    
}
struct newscat: Codable {
    //var id = ""
    var name : String
    var ID = 0
    
}


extension newscat {
    /// 获取 分类
    static func request(completion: @escaping ([newscat]?) -> Void ) {
        
        let provider = MoyaProvider<NetworkService2>()
        
        provider.request(.category) { (result) in
            switch result {
            case let .success(moyaResponse):
                
                do {
                    let decoder = JSONDecoder()
                    
                    let newsCatResponse = try decoder.decode(homeCateIndexResponse.self, from: moyaResponse.data)
                    
                    
                   
                        
                    //print("news目录",newsCatResponse.newsCat)
                    print("proCate网络请求newsCat目录数",newsCatResponse.newsCat.count)
                    //rprint("proCate网络请求newsCat目录",newsCatResponse.newsCat)
                    completion(newsCatResponse.newsCat)
                    
                } catch  {
                    print("proCate目录json解析错误:",error)
                }

                
            case .failure:
                print("网络错误")
                completion(nil)
            }
        }
        
    }
    
}

extension productcat {
    /// 获取 分类
    static func request(completion: @escaping ([productcat]?) -> Void ) {
        
        let provider = MoyaProvider<NetworkService2>()
        
        provider.request(.category) { (result) in
            switch result {
            case let .success(moyaResponse):
                
                do {
                    let decoder = JSONDecoder()
                    
                    let productCateResponse = try decoder.decode(homeCateIndexResponse.self, from: moyaResponse.data)

                    print("网络请求productCat目录",productCateResponse.productCat.count)
                    completion(productCateResponse.productCat)
                    
                } catch  {
                    print("目录json解析错误:",error)
                }
                
                
            case .failure:
                print("网络错误")
                completion(nil)
            }
        }
        
    }
    
}


//func getnewsarr(completion: @escaping ([String]?) -> Void ){
//    var arr3 : [String] = []
//    
//    newscat.request  { (cates) in
//        
//        //        let jsonString = """
//        //        {"newsCat":[{"ID":15,"name":"教育專欄"},{"ID":9,"name":"會員優惠"},{"ID":11,"name":"活動回顧"},{"ID":27,"name":"活動推介"},{"ID":21,"name":"煮食無難度"},{"ID":2,"name":"環保小先鋒"},{"ID":26,"name":"美容專欄"},{"ID":19,"name":"醫者父母心"}],"productCat":[{"ID":41,"name":"家居用品"},{"ID":38,"name":"家用電器"},{"ID":34,"name":"數碼配件"},{"ID":40,"name":"服裝飾物"},{"ID":36,"name":"精品"},{"ID":35,"name":"美容護理"},{"ID":42,"name":"課程"},{"ID":39,"name":"運動用品"}]}
//        //        """
//        
//        //        let jsonString = cates
//        //
//        //        let jsonData = jsonString.data(using: .utf8)!
//        //
//        //
//        //        struct homeCateIndexResponse: Codable {
//        //
//        //            var newsCat : [newscat]!
//        //        }
//        //
//        //
//        //        struct newscat: Codable {
//        //
//        //            var name : String
//        //            var ID = 0
//        //
//        //        }
//        
//        let products = cates
//        
//        for product in products! {
//            
//            arr3.append(product.name as String)
//        }
//        
//        //print("getnewsarr arr3数量:",arr3.count)
//        //print(arr3)
//        completion(arr3)
//    }
//    
//    
//    //return arr3
//}

func getproarr(completion: @escaping ([String]?) -> Void ){
    var proarr : [String] = []
    
    productcat.request  { (cates) in
        
        let products = cates
        
        for product in products! {

            proarr.append(product.name as String)
        }
        
        //print("getnewsarr arr3数量:",arr3.count)
        //print(arr3)
        completion(proarr)
    }

}



    /// 获取 分类
//func getprolisttest(completion: @escaping ([productcat]?) -> Void ) {
//    
//    
//    let provider = MoyaProvider<NetworkService2>()
//    
//    
//    
//    provider.request(.showproductCatList) { (result) in
//        switch result {
//        case let .success(moyaResponse):
//            
//            do {
//                let decoder = JSONDecoder()
//                
//                let catesIndexResponse = try decoder.decode(productCat3IndexResponse.self, from: moyaResponse.data)
//                
//                print("目录json",catesIndexResponse.newsCat.count)
//                print("目录productCat",catesIndexResponse.productCat)
//                //print("目录newsCat",catesIndexResponse.newsCat)
//                //completion(nil)
//                completion(catesIndexResponse.productCat)
//                
//            } catch  {
//                print("目录json解析错误:",error)
//            }
//            
//            
//        case .failure:
//            print("网络错误")
//            completion(nil)
//        }
//    }
//    
//}

